package com.example.mapper;

import com.example.entity.LoginResponse;
import com.example.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

@Mapper
public interface UserMapper {

    /**
     * 用户注册
     * @param user 用户信息
     */
    @Insert("insert into user(username, password, gender, profile, email, permission, register_time) " +
            "values (#{username},#{password},#{gender},#{profile},#{email},#{permission},#{registerTime})")
    void register(User user);

    @Select("select case when exists(select email from user where email=#{email}) then 1 else 0 end as result")
    Integer emailExist(String email);

    @Select("select password from user where username=#{username} and permission=#{permission}")
    String login(String username, Integer permission);

    @Select("select case when exists(select id from user where username=#{username}) then 1 else 0 end as result")
    boolean usernameExist(String username);

    @Select("select id from user where username=#{username}")
    Integer getIdByUsername(String username);

    @Select("select password from user where id=#{id}")
    String queryPasswordById(Integer id);

    @Update("update user set password=#{newPwd} where id=#{id}")
    void updatePassword(Integer id, String newPwd);

    @Select("select username,gender,email,profile from user where id=#{id}")
    LoginResponse findUserInfo(Integer id);

    void updateUserInfo(User user);

    @Update("update user set status = 1, login_time=#{localDateTime} where username=#{username}")
    void setStatus(String username, LocalDateTime localDateTime);

    @Update("update user set status = 0, logout_time=#{localDateTime} where id=#{id};")
    void setStatusOut(Integer id, LocalDateTime localDateTime);

    @Update("update user set profile = #{profile} where id=#{id};")
    void updateProfile(String profile, Integer id);
}